package com.wty.app.bthcar.adapter;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wty.app.bthcar.R;
import com.wty.app.bthcar.info.DataBean;
import com.wty.app.bthcar.info.DeviceBean;

import java.util.ArrayList;

public class DeviceListAdapter extends RecyclerView.Adapter<DeviceListAdapter.DeviceHolder> {
    public DeviceListAdapter(ArrayList<DeviceBean> pairedList) {
        this.deviceBeans = pairedList;
    }

    public void setDeviceBeans(ArrayList<DeviceBean> deviceBeans) {
        this.deviceBeans = deviceBeans;
    }

    OnClickListener mDeviceClickListener;

    private ArrayList<DeviceBean> deviceBeans;


    @NonNull
    @Override
    public DeviceHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.device_item, parent, false);
        return new DeviceHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull DeviceHolder holder, int position) {
        DeviceBean deviceBean = deviceBeans.get(position);
        holder.itemName.setText(deviceBean.getDeviceName());
        holder.itemMac.setText(deviceBean.getDeviceMac());
        Log.e("getPrintITem", deviceBean.getDeviceName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mDeviceClickListener != null) {
                    mDeviceClickListener.onClick(deviceBean);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return deviceBeans == null ? 0 : deviceBeans.size();
    }

    public void setOnItemClickListener(OnClickListener mDeviceClickListener) {
        this.mDeviceClickListener = mDeviceClickListener;
    }

    public void add(DeviceBean s) {
        if (this.deviceBeans == null) {
            this.deviceBeans = new ArrayList<>();
        }
        this.deviceBeans.add(s);
        notifyItemInserted(this.deviceBeans.size());
    }

    static class DeviceHolder extends RecyclerView.ViewHolder {
        TextView itemName;
        TextView itemMac;

        public DeviceHolder(@NonNull View itemView) {
            super(itemView);
            itemName = itemView.findViewById(R.id.device_name);
            itemMac = itemView.findViewById(R.id.device_mac);
        }
    }

    public interface OnClickListener {
        void onClick(DeviceBean bean);
    }
}
